package com.zkt.recommend.application.controller;

import cn.hutool.core.util.ObjUtil;
import com.alibaba.fastjson2.JSON;
import com.zkt.recommend.application.convert.RecommendUserPortraitDTOConverter;
import com.zkt.recommend.entity.RecommendUserPortraitDTO;
import com.zkt.recommend.common.entity.Result;
import com.zkt.recommend.domain.entity.RecommendUserPortraitBO;
import com.zkt.recommend.domain.service.RecommendUserPortraitDomainService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2025/3/27
 * @description 用户画像controller层
 **/

@RestController
@RequestMapping("/recommendUserPortrait")
@Slf4j
public class RecommendUserPortraitController {
	
	@Resource
	private RecommendUserPortraitDomainService recommendUserPortraitDomainService;
	
	/**
	 * 获取用户画像
	 *
	 * @param username 用户名/账号
	 * @return 是否成功
	 */
	@GetMapping("getUserPortrait")
	public Result<List<RecommendUserPortraitDTO>> getUserPortrait(@RequestParam(value = "username", required = false) String username) {
		try {
			if (log.isInfoEnabled()) {
				log.info("RecommendUserPortraitController.getUserPortrait.username:{}", username);
			}
			
			List<RecommendUserPortraitBO> recommendUserPortraitBOList = recommendUserPortraitDomainService.getUserPortrait(username);
			
			List<RecommendUserPortraitDTO> recommendUserPortraitDTOList = RecommendUserPortraitDTOConverter.INSTANCE.convertBoToDtoList(recommendUserPortraitBOList);
			
			return Result.ok(recommendUserPortraitDTOList);
		} catch (Exception e) {
			log.error("RecommendUserPortraitController.getUserPortrait.error:{}", e.getMessage(), e);
			return Result.fail("获取用户画像失败");
		}
	}
	
	/**
	 * 删除用户画像
	 *
	 * @param recommendUserPortraitDTO 删除条件
	 * @return 是否成功
	 */
	@PostMapping("remove")
	public Result<Boolean> remove(@RequestBody RecommendUserPortraitDTO recommendUserPortraitDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("RecommendUserPortraitController.remove.dto:{}", JSON.toJSONString(recommendUserPortraitDTO));
			}
			
			if (ObjUtil.isNull(recommendUserPortraitDTO.getUserId())) {
				throw new Exception("用户id不能为空");
			}
			
			RecommendUserPortraitBO recommendUserPortraitBO =
					RecommendUserPortraitDTOConverter.INSTANCE.convertDtoToBo(recommendUserPortraitDTO);
			
			return Result.ok(recommendUserPortraitDomainService.remove(recommendUserPortraitBO));
		} catch (Exception e) {
			log.error("RecommendUserPortraitController.remove.error:{}", e.getMessage(), e);
			return Result.fail("删除用户画像失败");
		}
	}
	
}